package android.com.app;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TabHost;

/* THIS IS THE TABBED ACTIVITY THAT IS CALLED WHEN AN IMAGE IS CLICKED FROM A GALLERY */

/* BY default the image is shown under the view tab and is loaded within this activity*/

public class imageviewer extends TabActivity {


	private TabHost mTabHost;
	private static final int GAL_ID = Menu.FIRST;
	private static final int CAM_ID = Menu.FIRST+1;
	private ImageView image;
	private Bitmap bmp;

	public void onCreate(Bundle savedInstanceState) {
		
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.apicimageview);
	    
	    /* Create tabs */
	    mTabHost = getTabHost();
	    mTabHost.addTab(mTabHost.newTabSpec("tab_view").setIndicator("View").setContent(R.id.imgdisplay));
	    mTabHost.addTab(mTabHost.newTabSpec("tab_map").setIndicator("Map").setContent(getMapIntent()));
	    mTabHost.addTab(mTabHost.newTabSpec("tab_tag").setIndicator("Tags").setContent(getTagsIntent()));
	    mTabHost.addTab(mTabHost.newTabSpec("tab_edittag").setIndicator("Edit Tags").setContent(geteditTagsIntent()));
	    mTabHost.addTab(mTabHost.newTabSpec("tab_aud").setIndicator("Audio").setContent(getAudioIntent()));
	    loadTabContents();
	    mTabHost.setCurrentTab(0);
	}
	
	/* Create the intent to load the activity in AUDIO tab */
	public Intent getAudioIntent(){
		Intent i = new Intent( this, audiohandler.class);
		// Here we pass the title of the image (.DATA field) so the called activity can query all the audio files attached to this image.
		i.putExtra("Title", getIntent().getExtras().getString("Title"));
		return i;
	}
	
	/* Create the intent to load the activity in MAP tab */
	public Intent getMapIntent(){
		//Create a new intent to call mapviewer activity. copy the geopoints from the current intent(passed to this activity) to the new intent
		Intent i = new Intent(this,mapviewer.class);
		i.putExtra("GeoPoint", getIntent().getExtras().getString("GeoPoint"));
		return i;
	
	}
	
	/* Create the intent to load the activity in TAGS tab */
	public Intent getTagsIntent(){
		
		
		Intent i = new Intent(this,TagActivity.class);
		// Here we pass the title of the image (.DATA field) so the called activity can query all the tags attached to this image.
		i.putExtra("Title", getIntent().getExtras().getString("Title"));
		return i;
	}

	public Intent geteditTagsIntent(){
		Intent i = new Intent(this, editTag.class);
		i.putExtra("Title", getIntent().getExtras().getString("Title"));
		i.putExtra("ImageData", getIntent().getExtras().getString("ImageData"));
		return i;
	}
	
	
	/* This initializes the default view tab*/

	private void loadTabContents(){
		Log.i("DEb","in "+String.valueOf(System.currentTimeMillis()));
		image = (ImageView) findViewById(R.id.imgdisplay);
		Bundle extras=getIntent().getExtras();     
		final String u = extras.getString("Title");
		Matrix mat=new Matrix();
		mat.postScale(1,1);
		if(u==null)
			Log.i("NULL","null");
		
		if(bmp==null) {
			BitmapFactory.Options bo = new BitmapFactory.Options();
			bo.inSampleSize=8;  
			bmp = BitmapFactory.decodeFile(u, bo);
			bmp=Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
			image.setImageBitmap(bmp);
			Log.i("DEbF","in "+String.valueOf(System.currentTimeMillis()));
		}
	}
	  
	// Set menu options
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, GAL_ID, 0, "Gallery");
        menu.add(0, CAM_ID, 0,  "Camera");
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case GAL_ID:
        	startActivity(new Intent( this, thumbviewer.class));
            return true;
        case CAM_ID:
        	startActivity(new Intent( this, aPics.class));
        	return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
	
	protected void onDestroy() {
    	super.onDestroy();
    	//Intent i = new Intent( this, thumbviewer.class);
		// Here we pass the title of the image (.DATA field) so the called activity can query all the audio files attached to this image.
		//startActivity(i);
    	//finish();
    	
    }

	protected void onPause() {
		System.gc();
		super.onPause();
		    }
	
	protected void onResume(){
		Log.i("DEb","in imgresume");
		super.onResume();
		   }


}